package net.dgg.rz.common.service;/**
 * Created by Administrator on 2018/5/7.
 */

import net.dgg.iboss.base.service.CmsService;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.utils.ValidateUtils;
import net.dgg.rz.production.exception.ProductOrderExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘阳
 * @ClassName <ScWorkDayService>
 * @despriction 生产工作日service
 * @create 2018/05/07 9:40
 **/
@Service
public class ZzWorkDayService {
    @Autowired
    private CmsService cmsService;

    /**
     * 获取下一个工作日的 23:59:59
     *
     * @param date
     * @return
     */
    public Date getNextWorkDateEnd(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String dateStr = this.cmsService.findAfterDate(fmt.format(calendar.getTime()), 1);
        Assert.hasText(dateStr, "工作日获取失败！");
        try {
            Date nextWorkDate = fmt.parse(dateStr);
            Calendar overCalendar = Calendar.getInstance();
            overCalendar.setTime(nextWorkDate);
            overCalendar.set(Calendar.HOUR_OF_DAY, 23);
            overCalendar.set(Calendar.MINUTE, 59);
            overCalendar.set(Calendar.SECOND, 59);
            overCalendar.set(Calendar.MILLISECOND, 0);
            return overCalendar.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("工作日时间转换出现异常！");
        }
    }
/*
    /**
     * 获取代入暂停时间后的 从某日开始x个工作日的日期
     *
     * @param start          计时开始时间
     * @param len            办理时间
     * @param suspendMiniSec 暂停 单位：毫秒
     * @return
     */
    /*public Date getWorkDateFrom(Date start, Integer len, Long suspendMiniSec) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        int h = calendar.get(Calendar.HOUR_OF_DAY), m = calendar.get(Calendar.MINUTE), s = calendar.get(Calendar.SECOND), ms = calendar.get(Calendar.MILLISECOND);
        Long all = h * 3600 * 1000L + m * 60 * 1000L + s * 1000L + len * 24 * 3600 * 1000L + suspendMiniSec, dayMs = 24 * 3600 * 1000L;
        Long ext = all % dayMs, days = all / dayMs;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = this.cmsService.findAfterDate(fmt.format(start), days.intValue());
        Assert.hasText(dateStr, "获取工作日失败！");
        Date date = null;
        try {
            date = fmt.parse(dateStr);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date);
            calendar1.add(Calendar.MILLISECOND, ext.intValue());
            return calendar1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            Assert.isTrue(false, "工作日期转换出现异常！");
        }
        return date;

    }*/

    /**
     * 2018年6月14日 16:30:09
     * 修改  先计算预计完结时间 再按自然日 顺延 暂停总时长
     *
     * @param start
     * @param len
     * @param suspendMiniSec
     * @return
     */
    public Date getWorkDateFrom(Date start, Integer len, Long suspendMiniSec) {
        Date date = this.getWorkDateFrom(start, len);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MILLISECOND, suspendMiniSec.intValue());
        return calendar.getTime();
    }

    /**
     * 获取从某日开始x个工作日的日期
     *
     * @param start 开始日期
     * @param len   天数
     * @return
     */
    public Date getWorkDateFrom(Date start, Integer len) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(start);
        Map map = new HashMap();
        map.put("date", fmt.format(start));
        map.put("days", len);
        String dateStr = this.cmsService.findAfterDate(fmt.format(start), len);
        Assert.hasText(dateStr, "获取工作日失败！");
        Date date = null;
        try {
            date = fmt.parse(dateStr);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date);
            calendar1.set(Calendar.HOUR_OF_DAY, calendarStart.get(Calendar.HOUR_OF_DAY));
            calendar1.set(Calendar.MINUTE, calendarStart.get(Calendar.MINUTE));
            calendar1.set(Calendar.SECOND, calendarStart.get(Calendar.SECOND));
            calendar1.set(Calendar.MILLISECOND, calendarStart.get(Calendar.MILLISECOND));
            return calendar1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            ValidateUtils.isTrue(false, ProductOrderExcption.class,"工作日期转换出现异常");
        }
        return date;
    }

    /**
     * 获取从某日开始x个自然日的日期
     *
     * @param start 开始日期
     * @param len   天数
     * @return
     */
    public Date getDateFrom(Date start, Integer len) {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(start);
        Calendar overCalendar = Calendar.getInstance();
        overCalendar.setTime(start);
        overCalendar.add(Calendar.DAY_OF_YEAR, len);
        overCalendar.set(Calendar.HOUR_OF_DAY, calendarStart.get(Calendar.HOUR_OF_DAY));
        overCalendar.set(Calendar.MINUTE, calendarStart.get(Calendar.MINUTE));
        overCalendar.set(Calendar.SECOND, calendarStart.get(Calendar.SECOND));
        overCalendar.set(Calendar.MILLISECOND, calendarStart.get(Calendar.MILLISECOND));
        return overCalendar.getTime();
    }


    /**
     * 获取代入暂停时间后的 从某日开始x个工作日的日期
     *
     * @param start          开始日期
     * @param len            天数
     * @param suspendMiniSec 暂停时长 单位: 毫秒
     * @return
     */
    private Date getDateFrom(Date start, Integer len, Long suspendMiniSec) {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(start);
        Calendar overCalendar = Calendar.getInstance();
        overCalendar.setTime(start);
        overCalendar.add(Calendar.DAY_OF_YEAR, len);
        overCalendar.add(Calendar.MILLISECOND, suspendMiniSec.intValue());
        return overCalendar.getTime();
    }

    /**
     * 计算日期 存在暂停时间时
     *
     * @param start          开始时间
     * @param len            天数
     * @param suspendMiniSec 暂停时长 ：毫秒
     * @param dateType       类型，常量定义于 WorkDayOrNaturalDayStatus
     * @return
     */
    public Date getDateFromStartByType(Date start, Integer len, Long suspendMiniSec, int dateType) {
        return ScProductConstant.WORKDAY_STATUS == dateType ?
                this.getWorkDateFrom(start, len, suspendMiniSec) : this.getDateFrom(start, len, suspendMiniSec);
    }

    /**
     * 计算日期
     *
     * @param start
     * @param len
     * @param dateType
     * @return
     */
    public Date getDateFromStartByType(Date start, Integer len, int dateType) {
        return ScProductConstant.WORKDAY_STATUS == dateType ?
                this.getWorkDateFrom(start, len) : this.getDateFrom(start, len);
    }


}
